package com.fyzk.activity.fragment;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fyzk.activity.BaskOrderRootActivity;
import com.fyzk.base.BaseFragment;
import com.fyzk.model.DocumentaryViewModel;
import com.tyjc.lottery.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangfeng
 * @create 2018/11/26 14:31
 * @Describe 大神推单界面
 */
@ContentView(R.layout.fragment_gold_document)
public class GoldDocumentaryFragment extends BaseFragment {
    @ViewInject(R.id.gold_documentary_tb)
    TabLayout mGoldDocumentaryTab;
    @ViewInject(R.id.tv_go_documentary)
    TextView tvGoDocumentary;
    @ViewInject(R.id.documentary_vp_gold)
    ViewPager mDocumentaryVpGold;
    private List<Fragment> mFragment = new ArrayList<>();
    private GoldDocumentaryAdapter mAdapter;
    private DocumentaryViewModel mDocumentaryViewModel;

    public static GoldDocumentaryFragment newInstance() {
        GoldDocumentaryFragment fragment = new GoldDocumentaryFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inject = x.view().inject(this, inflater, container);
        mDocumentaryViewModel = ViewModelProviders.of(getActivity()).get(DocumentaryViewModel.class);
        initData();
        initViews();
        initListener();
        return inject;
    }

    private void initData() {
//        mGoldDocumentaryTab.setMinimumHeight(200);
//        mGoldDocumentaryTab.setPadding(0, 50, 0, 0);
//        TabLayoutUnderline.setTabLayoutIndicator(mGoldDocumentaryTab);
    }

    private void initViews() {
        //跟单
        mFragment.add(DocumentaryRootFragment.newInstance());
//        mFragment.add(BaskOrderRootFragment.newInstance());
        //合买
//        mFragment.add(TogetherBuyHallFragment.newInstance());
        mAdapter = new GoldDocumentaryAdapter(getChildFragmentManager());
        mDocumentaryVpGold.setAdapter(mAdapter);
        mDocumentaryVpGold.setOffscreenPageLimit(3);
        mGoldDocumentaryTab.setupWithViewPager(mDocumentaryVpGold);
        mDocumentaryViewModel.getGotoBaskInASingle().observeForever(new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer integer) {
//                mDocumentaryVpGold.setCurrentItem(integer);
                Intent intent = new Intent(getActivity(), BaskOrderRootActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initListener() {
        tvGoDocumentary.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_go_documentary:
                mDocumentaryViewModel.onRequestGotoBaskInASingle();
                break;
        }
    }

    private class GoldDocumentaryAdapter extends FragmentStatePagerAdapter {
        private String[] mTitle = new String[]{"跟单",/*"晒单",*/ /*"合买"*/};

        public GoldDocumentaryAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mFragment.get(i);
        }

        @Override
        public int getCount() {
            return mFragment.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }
    }
}
